package com.example.aiyou.passwordmanage.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.aiyou.passwordmanage.R;
import com.example.aiyou.passwordmanage.activity.MainActivity;
import com.example.aiyou.passwordmanage.pojo.Password;

import java.util.List;

/**
 * Created by hello on 2017-09-08.
 */

public class PasswordAdpater extends RecyclerView.Adapter<PasswordAdpater.ViewHolder> implements View.OnClickListener,View.OnLongClickListener{
    private List <Password> passwords;
    private OnItemClickListener onItemClickListener = null;
    private MainActivity mainActivity;
    Resources res;
    String [] array;
    public PasswordAdpater(List<Password> passwords, MainActivity mainActivity) {
        this.passwords = passwords;
        this.mainActivity = mainActivity;
        res = mainActivity.getResources();
        array = res.getStringArray(R.array.spingarr);
    }

    @Override
    public boolean onLongClick(View view) {
        if (onItemClickListener != null) {
            //注意这里使用getTag方法获取position
            onItemClickListener.onLongClick(view,(int)view.getTag());
        }
        return true;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view,int position);
        void onLongClick(View view,int position);
    }
    @Override
    public void onClick(View view) {
        if (onItemClickListener != null) {
            //注意这里使用getTag方法获取position
            onItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private View iconView;
        private ImageView iconImage;
        private TextView iconName;
        private TextView iconType;
        private TextView iconNick;
        public ViewHolder(View view) {
            super(view);
            iconView = view;
            iconImage = (ImageView) view.findViewById(R.id.icon_image);
            iconName = (TextView) view.findViewById(R.id.icon_name);
            iconType = (TextView)view.findViewById(R.id.icon_type);
            iconNick = (TextView)view.findViewById(R.id.icon_nick);
        }
    }
    @Override
    public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
        View view = LayoutInflater.from( parent.getContext()) .inflate( R.layout.icon_name, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.iconView.setOnClickListener(this);
        holder.iconView.setOnLongClickListener(this);
        return holder;
    }
    @Override
    public void onBindViewHolder( ViewHolder holder, int position) {
        Password p = passwords.get(position);
        int iconId =  res.getIdentifier(p.getIcon(),"drawable",res.getResourcePackageName(R.drawable.qq));
        holder.iconImage.setImageResource(iconId);
        holder.iconName.setText(p.getName());
        holder.itemView.setTag(position);
        holder.iconType.setText(array[p.getType()]);
        holder.iconNick.setText(p.getNick());

    }
    @Override
    public int getItemCount() {
        if(passwords == null){
            return 0;
        }
        return passwords.size();
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
